<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    // 创建基类和选择器的设计模式
    // js基类
    var JavaScript = function () {
      this.intro = "Hey i'm javascript!"
    }
    JavaScript.prototype = {
      getMember: function () {
        return this.intro;
      }
    }
    // java基类
    var Java = function () {
      this.intro = "Hey i'm java!"
    }
    Java.prototype = {
      getMember: function () {
        return this.intro;
      }
    }
    //构造选择器
    var structurFactory = function (type) {
      switch (type) {
        case 'JavaScript':
          return new JavaScript();
          break;
        case 'Java':
          return new Java();
          break;
      }
    }
    var tester = structurFactory('JavaScript');
    console.log(tester.getMember());
    /**********************************************************/
    //创建对象模式，对函数的返回对象不断增加方法
    var createFactory = function (type, options) {
      var o = {};
      o.content = options;
      o.show = function () {
        console.log('显示方法');
      };
      if (type == 'alert') {
        console.log('alert方法');
      }
      if (type == 'prompt') {
        console.log('提示框方法');
      }
      if (type == 'confirm ') {
        console.log('提示框方法');
      }
      return o;
    }

    var user = createFactory('alert');
    user.show()
  </script>

</body>


</html>